package com.kellycheung.chicken_go_go;

import org.json.JSONObject;

import android.graphics.Color;

import com.kellycheung.chicken_go_go.BaseObject.GOAnchor;

public class GameReward extends GameScene {
	protected static final int I_EXTRA_CNT = 6;
	
	protected float fLeftMenuWidth;
	protected GMenu gmLeftMenu;
	protected GMenu gmRewardMenu;
	protected GameSceneStatus rewardStatus;
	protected TextObject toRewardDesc;
	protected GPerferences gpMenu;
	//protected int maxStage = 10;
	protected int currentStage;
	protected int iConis;
	protected boolean bNetworkPlayAgain;

	public GameReward( GameEngine p_geMain , String[] p_sParm ) {
		super( p_geMain , GameReward.I_EXTRA_CNT , p_sParm );
		// TODO Auto-generated constructor stub
		
		// Background
		GameObject bgGreen = new GameObject( this , 0 , GOAnchor.UpLeft , 0f , 0f);
		bgGreen.addAnimation( new int[]{ R.drawable.bg_green } );
		this.addBackGround( bgGreen );
		this.iLoadCnt++;

		this.rewardStatus = GameSceneStatus.valueOf( p_sParm[0] );
		this.currentStage = Integer.parseInt(p_sParm[1]);
		this.iConis = Integer.parseInt(p_sParm[2]);
		
		this.gpMenu = new GPerferences( p_geMain );
		this.gssStatus = GameSceneStatus.ghNormal;
		this.gssStatusNormal = GameSceneStatus.ghNormal;
		
		this.bNetworkPlayAgain = false;
		
		this.initRewardMenu();
	}
	
	@Override
	public void initVariables() {
		this.iResBGMusic = 0;
	}
	
	public void initSingleWinMenu() {
		GameObject reward = new GameObject( this , 0 , GOAnchor.Up );
       	reward.addAnimation( new int[]{ R.drawable.reward_happy }  );
       	reward.setCoord( this.geMain.gcScreenSize.fX/2 , 50f );
        this.addSceneObj( reward );
       	
 		GameObject dialog = new GameObject( this , 0 , GOAnchor.Up );
 		dialog.addAnimation( new int[]{ R.drawable.dialog_green } );
 		dialog.setCoord( this.geMain.gcScreenSize.fX/2 , this.geMain.gcScreenSize.fY - dialog.getHeight() - (20f*this.geMain.gcScreenSize.fY/1080) );
 		this.addSceneObj( dialog );
 	 	this.iLoadCnt++;
		
 	 	// Text reward desc
    	toRewardDesc = new TextObject( this , 0 , GOAnchor.Up , this.geMain.gcScreenSize.fX/2 , dialog.grBound.fUp+(50f*this.geMain.gcScreenSize.fY/1080)   , "Yeah! You win the game. \n You receive $ "+this.iConis );
    	toRewardDesc.iColor = Color.WHITE;
    	this.addSceneObj( toRewardDesc );
		this.iLoadCnt++;
		
		if ( this.bIsAnyBTConnectionCreated ) {
			this.gpMenu.setPerference( "gpTokenNum" , ( this.gpMenu.getPerference( "gpTokenNum" , 0 ) + this.iConis ) );
		}
		else {
			this.gpMenu.setPerference( "gpGoldNum" , ( this.gpMenu.getPerference( "gpGoldNum" , 0 ) + this.iConis ) );
		}
 	 	
		this.gpMenu.setPerference( "gpHappyLv" , ( this.gpMenu.getPerference( "gpHappyLv" , 100 ) - 2 ) );
		
 	 	if ( this.bIsAnyBTConnectionCreated ) {
 	 		this.askPlayAgain();
 	 	}
 	 	else {
	       	ClickableObj btnOK = new ClickableObj( this , 0 , GOAnchor.Center );
			btnOK.addAnimation( new int[]{ R.drawable.btn_ok } );
			btnOK.setCoord( this.geMain.gcScreenSize.fX - btnOK.getWidth() , this.geMain.gcScreenSize.fY - btnOK.getHeight() );
		    this.addSceneObj( btnOK );
		    this.addClickableObj( btnOK );
			this.iLoadCnt++;
			
			btnOK.setBtnClickAct( new ButtonFunction() {
				@Override
				public String[] call() throws Exception {
					goHomePage();
					return null;
				}
			});
 	 	}
		
 	 	if ( this.gpMenu.getPerference( "gpRaceStage" , 1 ) + 1 <= this.gpMenu.getPerference( "gpMaxStage" , 10 ) ) {
 	 		if ( this.gpMenu.getPerference( "gpRaceStage" , 1 ) == this.currentStage ) {
 	 			this.gpMenu.setPerference( "gpRaceStage" , this.currentStage + 1 );
 	 		}
 	 	}
	}
	
	public void initSingleLossMenu() {
		GameObject reward = new GameObject( this , 0 , GOAnchor.Up );
       	reward.addAnimation( new int[]{ R.drawable.reward_sad } );
       	reward.setCoord( this.geMain.gcScreenSize.fX/2 , (50f*this.geMain.gcScreenSize.fY/1080) );
        this.addSceneObj( reward );
       	
 		GameObject dialog = new GameObject( this , 0 , GOAnchor.Up );
 		dialog.addAnimation( new int[]{ R.drawable.dialog_lose } );
 		dialog.setCoord( this.geMain.gcScreenSize.fX/2 , this.geMain.gcScreenSize.fY - dialog.getHeight() - (20f*this.geMain.gcScreenSize.fY/1080) );
 		this.addSceneObj( dialog );
 	 	this.iLoadCnt++;

		this.gpMenu.setPerference( "gpHappyLv" , ( this.gpMenu.getPerference( "gpHappyLv" , 100 ) - 5 ) );
		
 	 	if ( this.bIsAnyBTConnectionCreated ) {
 	 		this.askPlayAgain();
 	 	}
 	 	else {
	       	ClickableObj btnOK = new ClickableObj( this , 0 , GOAnchor.Center );
			btnOK.addAnimation( new int[]{ R.drawable.btn_ok } );
			btnOK.setCoord( this.geMain.gcScreenSize.fX - btnOK.getWidth() , this.geMain.gcScreenSize.fY - btnOK.getHeight() );
		    this.addSceneObj( btnOK );
		    this.addClickableObj( btnOK );
			this.iLoadCnt++;
			
			btnOK.setBtnClickAct( new ButtonFunction() {
				@Override
				public String[] call() throws Exception {
					goHomePage();
					return null;
				}
			});
 	 	}
	}
	
	public void initRewardMenu() {
		if ( this.rewardStatus == GameSceneStatus.grGameWin ) {
			this.initSingleWinMenu();
		}
		else {
			this.initSingleLossMenu();
		}
	}
	
	public void goHomePage() {
		String[] parm = new String[1];
		
		if ( this.bNetworkPlayAgain && this.bIsAnyBTConnectionCreated ) {
			parm[0] = "Again";
		}
		else {
			parm[0] = "";
		}
		
		synchronized ( this.geMain.shMain ) {
			this.geMain.addToNextScene( new GameHome( this.geMain , parm ) );
		}
	}
	
	@Override
	public void readMsgPre() {
		try {
			if ( this.bIsAnyBTConnectionCreated ) {
				if ( !this.geMain.gnhHandler.gnbtHandler.bIsServerSide ) {
					String msg = this.geMain.gnhHandler.gnbtHandler.getMsg();
					
					if ( msg != null ) {
						JSONObject jo = new JSONObject( msg );
						
						if ( jo != null && jo.has( "bNetworkPlayAgain" ) ) {
							if ( jo.get( "bNetworkPlayAgain" ).toString().equals( "true" ) ) {
								this.geMain.gnhHandler.gnbtHandler.pollMsg();
								this.bNetworkPlayAgain = true;
								goHomePage();
							}
							else {
								this.geMain.gnhHandler.gnbtHandler.disconnect();
								goHomePage();
							}
						}
					}
				}
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GameReward.readMsgPre" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void askPlayAgain() {
 		if ( this.geMain.gnhHandler.gnbtHandler.bIsServerSide ) {
 	 		this.geMain.showYesNoDialog(
 	 			this.geMain.sPackageName
 	 			,"Play Again"
 	 			,new ButtonFunction() {
 	 				@Override
 	 				public String[] call() throws Exception {
 	 					// TODO Auto-generated method stub
 	 					bNetworkPlayAgain = true;
 	 					
 	 					geMain.gnhHandler.gnbtHandler.pushMsg( new JSONObject().put( "bNetworkPlayAgain" , "true" ) );
 	 					
 	 					goHomePage();
 	 					
 	 					return null;
 	 				}
 	 			}
 	 			,new ButtonFunction() {
 	 				@Override
 	 				public String[] call() throws Exception {
 	 					// TODO Auto-generated method stub
 	 					geMain.gnhHandler.gnbtHandler.pushMsg( new JSONObject().put( "bNetworkPlayAgain" , "false" ) );
 	 					
 	 					geMain.gnhHandler.gnbtHandler.disconnect();
 	 					
 	 					goHomePage();
 	 					
 	 					return null;
 	 				}
 	 			}
 	 			,1000
 	 		);
 		}
	}
}
